﻿// 用common.js必须加上Feng.addCtx("${ctxPath}");
Feng.info = function (info) {
    top.layer.msg(info);
};
Feng.success = function (info) {
    top.layer.alert(info, {icon: 1});
};
Feng.successMsg = function (info) {
    top.layer.msg(info, {icon: 1});
};
Feng.errorMsg = function (info) {
    top.layer.msg(info, {icon: 2});
};
Feng.error = function (info) {
    top.layer.alert(info, {icon: 2,title:"错误"});
};
Feng.confirm = function (tip, ensure) {
    top.layer.confirm(tip, {
        skin: 'layui-layer-admin',
    }, function (index) {
        ensure(index);
        top.layer.close(index);
    });
};
/**
 * 加载中遮罩
 */
Feng.loading = function(msg){
    if(msg == ''){
        return top.layer.msg('加载中请稍后...', {icon: 16, shade: [0.5, '#393D49'], time: 0});
    } else {
        return top.layer.msg(msg , {icon: 16, shade: [0.5, '#393D49'], time: 0});
    }

}
Feng.confirmWithIcon = function (iconIndex,tip, ensure) {
    top.layer.confirm(tip, {
        skin: 'layui-layer-admin',icon:iconIndex
    }, function (index) {
        ensure(index);
        top.layer.close(index);
    });
};

Feng.prompt = function (tip, ensure) {
    layer.prompt({
        formType: 2,
        value: '',
        title: tip
    }, function (value, index, elem) {
        ensure(value);
        layer.close(index);
    });
};


Feng.currentDate = function () {
    // 获取当前日期
    var date = new Date();

    // 获取当前月份
    var nowMonth = date.getMonth() + 1;

    // 获取当前是几号
    var strDate = date.getDate();

    // 添加分隔符“-”
    var seperator = "-";

    // 对月份进行处理，1-9月在前面添加一个“0”
    if (nowMonth >= 1 && nowMonth <= 9) {
        nowMonth = "0" + nowMonth;
    }

    // 对月份进行处理，1-9号在前面添加一个“0”
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }

    // 最后拼接字符串，得到一个格式为(yyyy-MM-dd)的日期
    return date.getFullYear() + seperator + nowMonth + seperator + strDate;
};
Feng.underLineToCamel = function (str) {
    var strArr = str.split('_');
    for (var i = 1; i < strArr.length; i++) {
        strArr[i] = strArr[i].charAt(0).toUpperCase() + strArr[i].substring(1);
    }
    var result = strArr.join('');
    return result.charAt(0).toUpperCase() + result.substring(1);
}
Feng.getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    } else {
        return null;
    }
};
//利用插件加载图片
Feng.loadImageUrl = function (id, imageArry, name) {
    var imageList = '';
    for (var i = 0; i < imageArry.length; i++) {
        if (imageArry[i] != '' && imageArry[i] != undefined) {
            imageList += '<li style="display: inline-block;margin-right: 10px;"><a href="javascript:void(0);" class="layui-btn layui-btn-normal layui-btn-xs" onclick="Feng.viewImage(this);" data-src="' + Feng.ctxPath + '/dfsfile/download/image?filePath=' + imageArry[i] + '"  alt="' + name + (i + 1) + '">' + name + (i + 1) + '</a></li>';
        }
    }
    $("#" + id).append(imageList);
}

Feng.viewImage = function (obj) {
    var filePath = $(obj).attr("data-src");
    var startIndex = filePath.lastIndexOf(".");
    var fileType;
    if (startIndex != -1) {
        fileType = filePath.substring(startIndex + 1, filePath.length).toLowerCase();
    }

    if (fileType.toLowerCase() == 'pdf') {
        var html = "<iframe type='application/pdf' src=" + $(obj).attr("data-src") + " width=100%  height='100%'></iframe> "
        // var html = '<a class="media" href="'+filePath+'"></a>'
        //var html = '<embed src="'+ filePath +'" type="application/pdf" width="100%" height="100%">';
        $("#imageView").html(html);
        //$('#imageView .media').media({width:'100%',height:300});

    } else {
        var html = '<img id="image" data-original="' + $(obj).attr("data-src") + '" src="' + Feng.ctxPath + '/assets/common/images/bg_login2.png" alt="' + $(obj).attr("alt") + '" style="height: 300px">'
        $("#imageView").html(html);
        $('#image').viewer({
            url: 'data-original',
            inline: true,
            navbar: false
        });
    }
}

Feng.downLoad = function (orderNo, productCode,table,id) {
    layer.load(2, {
        shade: [0.1, 'rgb(0, 0, 0)'] //0.1透明度的白色背景
    });
    $.fileDownload(Feng.ctxPath + '/downLoadFile', {
        httpMethod: 'GET',
        data: {orderNo: orderNo, productCode: productCode},
        prepareCallback: function (url) {
            layer.msg("文件下载中...");

        }, abortCallback: function (url) {
            // 异常终止
            layer.msg("文件下载异常！！");
            layer.closeAll('loading');
        },
        successCallback: function (url) {
            layer.msg("文件下载成功！！");
            layer.closeAll('loading');
            table.reload(id);
        },
        failCallback: function (html, url) {
            layer.msg("文件下载失败！！");
            layer.closeAll('loading');
        }

    });
}
/**
 * 批量下载
 */
Feng.downLoadBath = function (url, data, table, id) {
    layer.load(2, {
        shade: [0.1, 'rgb(0, 0, 0)'] //0.1透明度的白色背景
    });
    $.fileDownload(url, {
        httpMethod: 'POST',
        data: data,
        prepareCallback: function (url) {
            layer.msg("文件下载中...");

        }, abortCallback: function (url) {
            // 异常终止
            layer.msg("文件下载异常！！");
            layer.closeAll('loading');
        },
        successCallback: function (url) {
            layer.msg("文件下载成功！！");
            layer.closeAll('loading');
            table.reload(id);
        },
        failCallback: function (html, url) {
            layer.msg("文件下载失败！！");
            layer.closeAll('loading');
        }

    });
}

Feng.infoDetail = function (title, info) {
    var display = "";
    if (typeof info === "string") {
        display = info;
    } else {
        if (info instanceof Array) {
            for (var x in info) {
                display = display + info[x] + "<br/>";
            }
        } else {
            display = info;
        }
    }
    top.layer.open({
        title: title,
        type: 1,
        skin: 'layui-layer-rim', //加上边框
        area: ['950px', '600px'], //宽高
        content: '<div style="padding: 20px;">' + display + '</div>'
    });
};
Feng.zTreeCheckedNodes = function (zTreeId) {
    var zTree = $.fn.zTree.getZTreeObj(zTreeId);
    var nodes = zTree.getCheckedNodes();
    var ids = "";
    for (var i = 0, l = nodes.length; i < l; i++) {
        ids += "," + nodes[i].id;
    }
    return ids.substring(1);
};
Feng.closeAllLoading = function () {
    layer.closeAll('loading');
};

// 以下代码是配置layui扩展模块的目录，每个页面都需要引入
layui.config({
    base: Feng.ctxPath + '/assets/common/module/'
}).extend({
    formSelects: 'formSelects/formSelects-v4',
    treetable: 'treetable-lay/treetable',
    dropdown: 'dropdown/dropdown',
    notice: 'notice/notice',
    step: '../module/step-lay/step',
    steps: '../module/steps/steps',
    dtree: 'dtree/dtree',
    citypicker: 'city-picker/city-picker',
    tableSelect: 'tableSelect/tableSelect',
    selectPlus: '../../expand/module/selectPlus/selectPlus',
    ax: '../../expand/module/ax/ax',
    ztree: '../../expand/module/ztree/ztree-object',
    cron: 'cron/cron',
}).use(['admin'], function () {
    var $ = layui.jquery;
    var admin = layui.admin;

    // 移除loading动画
    setTimeout(function () {
        admin.removeLoading();
    }, window == top ? 600 : 100);

    //注册session超时的操作
    $.ajaxSetup({
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        complete: function (XMLHttpRequest, textStatus) {

            //通过XMLHttpRequest取得响应头，sessionstatus，
            var sessionstatus = XMLHttpRequest.getResponseHeader("sessionstatus");
            if (sessionstatus === "timeout") {

                //如果超时就处理 ，指定要跳转的页面
                window.location = Feng.ctxPath + "/global/sessionError";
            }
        }
    });

});